//
//  ContextMenuVC.m
//  PilotSeries
//
//  Created by 黄立强 on 16/4/19.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "ContextMenuVC.h"
#import "XMGLRTableView.h"
#import "MenuCategory.h"
#import <MJExtension.h>
@interface ContextMenuVC ()<XMGLRTableViewDataSource, XMGLRTableViewDelegate>

/** 类别数据 */
@property (nonatomic, strong) NSArray *categories;

@end

@implementation ContextMenuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加左右tableView的控件
    XMGLRTableView *lrTableView = [XMGLRTableView lrTableView];
    lrTableView.frame = self.view.bounds;
    lrTableView.dataSource = self;
    lrTableView.delegate = self;
    [self.view addSubview:lrTableView];
}

#pragma mark - 实现XMGLRTableView的数据源方法
- (NSInteger)numberOfRowsInLeftTableViewWithXMGLRTableView:(XMGLRTableView *)lrTableView
{
    return self.categories.count;
}

- (NSString *)lrTableView:(XMGLRTableView *)lrTableView leftTitleInRow:(NSInteger)row
{
    MenuCategory *category = self.categories[row];
    
    // 2.将模型数据的name属性
    return category.name;
}

- (NSArray *)lrTableView:(XMGLRTableView *)lrTableView subdataOfRow:(NSInteger)row
{

     MenuCategory *category = self.categories[row];
    // 2.返回子数据
    return category.submenus;
}
-(NSString *)lrTableView:(XMGLRTableView *)lrTableView withRightArr:(NSArray *)datas rightTitleInRow:(NSInteger)row{
    NSInteger leftSelectIndex  =  lrTableView.leftTableView.indexPathForSelectedRow.row;
     MenuCategory *category = self.categories[leftSelectIndex];
   MenuCategory *subcategory =  datas[row];
    if([subcategory.name integerValue]>1){
        return [NSString stringWithFormat:@"%@ %@s",subcategory.name,category.name];
    }else{
        return [NSString stringWithFormat:@"%@ %@",subcategory.name,category.name];
    }
    
}

-(NSString *)lrTableView:(XMGLRTableView *)lrTableView imageNameForRow:(NSInteger)row{
    return nil;
}

- (NSString *)lrTableView:(XMGLRTableView *)lrTableView highImageNameForRow:(NSInteger)row{
    return nil;
}

#pragma mark - 实现XMGLRTableView的代理方法
- (void)lrTableView:(XMGLRTableView *)lrTableView selectedLeftIndex:(NSInteger)leftIndex
{
    MenuCategory *category = self.categories[leftIndex];
    // 2.判断是否有子类别
    if (!category.submenus) {
        // 2.1.通知携带的信息
        NSDictionary *userInfo = @{LQCategoryDidChangeNotificationCategoryKey : category};
        
        // 2.2.发出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:LQCategoryDidChangeNotification object:nil userInfo:userInfo];
       
    }
}

- (void)lrTableView:(XMGLRTableView *)lrTableView selectedLeftIndex:(NSInteger)leftIndex andRightIndex:(NSInteger)rightIndex
{
      MenuCategory *category = self.categories[leftIndex];
     MenuCategory *subCategory = category.submenus[rightIndex];
    // 2.发出通知
    // 2.1.通知携带的信息
    NSDictionary *userInfo = @{LQCategoryDidChangeNotificationCategoryKey : category,
                               LQCategoryDidChangeNotificationSubcategoryKey : subCategory};
    
    // 2.2.发出通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LQCategoryDidChangeNotification object:nil userInfo:userInfo];
}

- (BOOL )lrTableView:(XMGLRTableView *)lrTableView submenusForRow:(NSInteger)row{
      MenuCategory *category = self.categories[row];

    return !category.submenus?NO:YES;

}

#pragma mark - 懒加载代码
- (NSArray *)categories
{
    if (_categories == nil) {
        _categories = [MenuCategory mj_objectArrayWithFilename:@"menucategory.plist"];
    }
    return _categories;
}
@end
